{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "ChatBot.ipynb",
      "version": "0.3.2",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "mrbY6e6B74t0",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Description: This is a chatbot program\n",
        "\n",
        "#There are broadly two variants of chatbots: Rule-Based and Self learning.\n",
        "#Rule-based approach, a bot answers questions based on some rules on which it is trained on\n",
        "#Self learning bots are the ones that use some Machine Learning-based approach to chat\n",
        "\n",
        "#Resource: (1) https://towardsdatascience.com/build-your-first-chatbot-using-python-nltk-5d07b027e727\n",
        "#          (2) https://api.coinmarketcap.com/v1/ticker/bitcoin/\n",
        "#          (3) https://api.coinmarketcap.com/v1/ticker/\n",
        "#          (4) https://realpython.com/python-bitcoin-ifttt/"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gS4Xxzvfkkuc",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from nltk.chat.util import Chat, reflections\n",
        "import requests\n",
        "import time\n",
        "from datetime import datetime"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5leZIGA2k0n6",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#The URL Ticker to get the .json files of the crypto currencies\n",
        "TICKER_URL = 'https://api.coinmarketcap.com/v1/ticker/'"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HC8kLbiPmzg7",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Function to get the latest crypto currency price of a specific 'crypto' e.g bitcoin, litecoin, etc.\n",
        "# crypto = {bitcoin, litecoin, etherium, ...}\n",
        "def get_latest_crypto_price( crypto ):\n",
        "    response = requests.get(TICKER_URL+crypto+'/')\n",
        "    response_json = response.json()\n",
        "    # Convert the price to a floating point number\n",
        "    return float(response_json[0]['price_usd'])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4hS8oyubkpGK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Pairs is a list of patterns and responses.\n",
        "pairs = [\n",
        "    [\n",
        "        r\"my name is (.*)\",\n",
        "        [\"Hello %1, How are you today ?\",]\n",
        "    ],\n",
        "     [\n",
        "        r\"(.*) (much|price) (.*)(bitcoin|btc)(.*)\",\n",
        "        [\"The price of bitcoin when you started this chat was \"+ \"\".join( str(get_latest_crypto_price( 'bitcoin'))),]\n",
        "    ],\n",
        "    [\n",
        "        r\"what is the price of (bitcoin-cash|bch)(.*)\",\n",
        "        [\"The price of bitcoin-cash when you started this chat was  \"+ \"\".join( str(get_latest_crypto_price( 'bitcoin-cash'))),]\n",
        "    ],\n",
        "    [\n",
        "        r\"(.*) (much|price) (.*) (litecoin|ltc)(.*)\",\n",
        "        [\"The price of litecoin when you started this chat was  \"+ \"\".join( str(get_latest_crypto_price( 'litecoin'))),]\n",
        "    ],\n",
        "    [\n",
        "        r\"(.*) (much|price) (.*)(ethereum|eth)(.*)\",\n",
        "        [\"The price of ethereum when you started this chat was  \"+ \"\".join( str(get_latest_crypto_price( 'ethereum'))),]\n",
        "    ],\n",
        "    [\n",
        "        r\"(.*)help(.*) \",\n",
        "        [\"I can help you \",]\n",
        "    ],\n",
        "     [\n",
        "        r\"(.*) your name ?\",\n",
        "        [\"My name is J.A.R.V.I.S like in Iron Man, but you can just call me Jarvis and I'm a chatbot ?\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"how are you ?\",\n",
        "        [\"I'm doing very well\\nHow about You ?\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"sorry (.*)\",\n",
        "        [\"Its alright\",\"Its OK, never mind that\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"i'm (.*) doing good\",\n",
        "        [\"Nice to hear that\",\"Alright, great !\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"(hi|hey|hello|hola|holla)(.*)\",\n",
        "        [\"Hello\", \"Hey there\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"what (.*) want ?\",\n",
        "        [\"Make me an offer I can't refuse\",]\n",
        "        \n",
        "    ],\n",
        "    [\n",
        "        r\"(.*) created ?\",\n",
        "        [\"randerson112358 created me using Python's NLTK library \",\"top secret ;)\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"(.*) (location|city) ?\",\n",
        "        ['Tokyo, Japan',]\n",
        "    ],\n",
        "    [\n",
        "        r\"how is the weather in (.*)?\",\n",
        "        [\"Weather in %1 is amazing like always\",\"It's hot here in %1\",\"It's chilli here in %1\", \"In %1 there is a 50% chance of rain\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"i work (in|at) (.*)?\",\n",
        "        [\"%1 is an amazing company, I have heard about it.\",]\n",
        "    ],\n",
        "[\n",
        "        r\"(.*)raining in (.*)\",\n",
        "        [\"No rain in the past 4 days here in %2\",\"In %2 there is a 50% chance of rain\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"is it (.*) in (.*)\",\n",
        "        [\"No its not %1 in %2\",\"It could be\", \"Yes its %1 in %2\"]\n",
        "    ],\n",
        "    [\n",
        "        r\"how (.*) health (.*)\",\n",
        "        [\"Health is very important, but I am a computer, so I don't need to worry about my health \",]\n",
        "    ],\n",
        "    [\n",
        "        r\"(.*)(sports|game|sport)(.*)\",\n",
        "        [\"I'm a very big fan of Basketball\",]\n",
        "    ],\n",
        "    [\n",
        "        r\"who (.*) sportsperson ?\",\n",
        "        [\"Messy\",\"LeBron\", \"D-Wade\"]\n",
        "],\n",
        "    [\n",
        "        r\"who (.*) (moviestar|actor|actress)?\",\n",
        "        [\"Zendaya\"]\n",
        "],\n",
        "    [\n",
        "        r\"quit\",\n",
        "        [\"Bye for now. See you soon :) \",\"It was nice talking to you. See you soon :)\"]\n",
        "],\n",
        "]"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "c2YiS_52kspi",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#A Function to run the chatbot\n",
        "def chatty():\n",
        "  print(\"Hi, I'm J.A.R.V.I.S and I want to help and chat with you ! \\nPlease type lowercase English language to start a conversation. Type quit to leave \") #default message at the start\n",
        "  chat = Chat(pairs,reflections )\n",
        "  chat.converse()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "njfkYphvktYi",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 555
        },
        "outputId": "44b48f3d-b1c3-4d60-c833-53b232c2cab0"
      },
      "source": [
        "#Run the chatbot\n",
        "chatty()"
      ],
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Hi, I'm J.A.R.V.I.S and I want to help and chat with you ! \n",
            "Please type lowercase English language to start a conversation. Type quit to leave \n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "error",
          "ename": "KeyboardInterrupt",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m"
            
          ]
        }
      ]
    }
  ]
}
